package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.AuthGrantDTO;
import net.wanho.dto.RoleDTO;
import net.wanho.dto.UserInfoDTO;
import net.wanho.po.AuthInfo;
import net.wanho.po.Role;
import net.wanho.po.UserInfo;
import net.wanho.service.RoleService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.PageInfo;
import net.wanho.vo.R;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;
    @RequestMapping("/role-list")
    public R list() {
       List<Role> list = roleService.findlist();
       return R.success(list);
    }

    @GetMapping("/role-page-list")
    public R getUserInfo(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize,
                         RoleDTO roleDTO) {
        PageInfo<Role> pageInfo = roleService.listPage(pageNum, pageSize, roleDTO);
        return R.success(pageInfo);
    }

    @PostMapping("/role-add")
    public R addRole(@RequestBody Role role) {
        role.setCreateBy(CurrentUtil.getUserId());
        role.setCreateTime(new Date());
        roleService.save(role);
        return R.success("添加角色成功");
    }
    @PutMapping("/role-state-update")
    public R updateRoleState(@RequestBody Role role) {
        role.setUpdateBy(CurrentUtil.getUserId());
        role.setUpdateTime(new Date());
        roleService.updateById(role);
        return R.success("修改角色状态成功");
    }
    @GetMapping("/{id}")
    public R getRoleById(@PathVariable("id") Integer id) {
        Role role = roleService.getById(id);
        return R.success(role);
    }
    @PutMapping("/role-update")
    public R updateRole(@RequestBody Role role) {
        role.setUpdateBy(CurrentUtil.getUserId());
        role.setUpdateTime(new Date());
        roleService.updateById(role);
        return R.success("角色修改成功");
    }
    @DeleteMapping("/role-delete/{id}")
    public R deleteBatch(@PathVariable("id") Integer id) {
        roleService.removeById(id);
        return R.success("删除成功");
    }
    @GetMapping("/role-auth")
    public R getRoleAuth(Integer roleId) {
        List<Integer> authIdList = roleService.getRoleAuthIdList(roleId);
        return R.success(authIdList);
    }
    @PutMapping("/auth-grant")
    public R grantAuth(@RequestBody AuthGrantDTO authGrantDTO) {
        roleService.updateAuth(authGrantDTO);
        return R.success("分配权限成功");
    }
    /**
     * 导出角色数据
     */
    @GetMapping("/exportTable")
    public R exportTable(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "5") int pageSize,
                         RoleDTO roleDTO) {
        PageInfo<Role> pageInfo = roleService.listPage(pageNum, pageSize, roleDTO);
        return R.success(pageInfo.getResultList());

    }


}
